package handlers

import (
	"eco-mall-mgr/cache"
	"eco-mall-mgr/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RoleNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func UserRoleList(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE user_roles.uid=1
	*/

	var role []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid=?", uid).
		Debug().
		Find(&role)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": role,
	})
	return
}

func RoleNodeList(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		WHERE role_nodes.rid =1
	*/

	var node []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid =?", rid).
		Debug().
		Find(&node)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}

func GoodAdd(c *gin.Context) {
	goodNo := c.PostForm("good_no")
	goodName := c.PostForm("good_name")
	goodPrice, _ := strconv.Atoi(c.PostForm("good_price"))
	goodNum, _ := strconv.Atoi(c.PostForm("good_num"))

	if goodNo == "" || goodName == "" || goodPrice == 0 || goodNum == 0 {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Good{
		GoodNo:    goodNo,
		GoodName:  goodName,
		GoodPrice: goodPrice,
		GoodNum:   goodNum,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func OrderAdd(c *gin.Context) {
	orderNo := c.PostForm("order_no")
	orderName := c.PostForm("order_name")
	manInfo := c.PostForm("man_info")
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	if orderNo == "" || orderName == "" || manInfo == "" || gid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Order{
		OrderNo:   orderNo,
		OrderName: orderName,
		ManInfo:   manInfo,
		Gid:       gid,
	}

	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
		SELECT
		orders.id as OrderId,
		goods.good_no as GoodNo,
		goods.good_name AS GoodName,
		goods.good_price AS GoodPrice,
		goods.good_num AS GoodNum,
		orders.order_no as OrderNo,
		orders.order_name AS OrderName,
		orders.man_info AS ManInfo
		FROM
		orders
		LEFT JOIN goods ON orders.gid = goods.id
		WHERE orders.id =1
	*/

	var orders []models.GoodOrderList

	redis_key := "order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Select(
				"orders.id as OrderId",
				"goods.good_no as GoodNo",
				"goods.good_name AS GoodName",
				"goods.good_price AS GoodPrice",
				"goods.good_num AS GoodNum",
				"orders.order_no as OrderNo",
				"orders.order_name AS OrderName",
				"orders.man_info AS ManInfo",
			).
			Offset(offset).
			Limit(size).
			Debug().
			Find(&orders)
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orders)
	}
	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"order": orders,
		},
	})
	return
}

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var orders []models.GoodOrderList

	redis_key := "order_detail"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Select(
				"orders.id as OrderId",
				"goods.good_no as GoodNo",
				"goods.good_name AS GoodName",
				"goods.good_price AS GoodPrice",
				"goods.good_num AS GoodNum",
				"orders.order_no as OrderNo",
				"orders.order_name AS OrderName",
				"orders.man_info AS ManInfo",
			).
			Where("orders.id =?", id).
			Debug().
			Find(&orders)
		jsonData, _ := json.Marshal(orders)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &orders)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": orders,
	})
	return
}
